package tester;

import java.util.List;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: aleck
 * Date: 2007-9-21
 * Time: 19:52:33
 * To change this template use File | Settings | File Templates.
 */
public class Grade {
  public final List<TestCaseResult> results = new ArrayList<TestCaseResult>();
  private StringBuilder resultString = new StringBuilder();
  private long timeUsed = 0;
  private int gainScore = 0;
  private boolean submitted = false;

  public Grade() {
  }

  public Grade(boolean submitted) {
    this.submitted = submitted;
  }

  public void initialize() {
    timeUsed = 0;
    resultString = new StringBuilder();
    gainScore = 0;
    submitted = false;
  }

  public boolean isTested() {
    return (results.size() > 0);
  }

  public void add(TestCaseResult tcr) {
    submitted = true;
    results.add(tcr);
    resultString.append(TestCaseResult.getExitCodeShortcut(tcr));
    timeUsed += tcr.timeUsed;
    gainScore += tcr.score;
  }

  public String getResultString() {
    return resultString.toString();
  }

  public long getTimeUsed() {
    return timeUsed;
  }

  public double getCorrectRatio()  {
    if (results.size() == 0)
      return 0;
    else
      return (double)gainScore / results.size();
  }

  public void clear() {
    results.clear();
    timeUsed = 0;
    gainScore = 0;
    resultString = new StringBuilder();
  }

  public String toString() {
    if (isTested())
      return resultString.toString() +
          " (" + (double)timeUsed / 1000 + "s, " +
          gainScore / results.size() + "%)";
    else if (submitted)
      return "Not Tested";
    else
      return "Not Submitted";
  }

  public boolean isSubmitted() {
    return submitted;
  }

  public void setSubmitted() {
    submitted = true;
  }

  public void setNotSubmitted() {
    submitted = false;
  }

  public void setTimeUsed(long timeUsed) {
    this.timeUsed = timeUsed;
  }

  public void setResultString(String resultString) {
    this.resultString = new StringBuilder(resultString);
  }
}
